package com.zhuchaooasystem.adapter.holder;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.ui.home.fragment.oa.daily.NewDailyActivity;
import com.zhuchaooasystem.utils.ScreenUtils;

import java.util.List;

/**
 * @author yinsi
 */
public class NewDailyHolder extends BaseViewHolder<String> {

    //----
    private RelativeLayout relative_root;
    private RelativeLayout relative_rootview;
    private RelativeLayout relative_checkbox;
    private ImageView  iv_delete;
    private ImageView iv_cover;
    //----
    private NewDailyActivity activity;
    private Context mContext;
    private List<String> imageList;
    private int image_height;
    private int image_height_root;


    public NewDailyHolder(ViewGroup parent, NewDailyActivity activity) {
        super(parent, R.layout.item_activity_new_daily_image);
        this.activity = activity;
        mContext = getContext();
        //----
        relative_root = $(R.id.item_act_new_daily_image_relative_root);
        relative_rootview = $(R.id.item_act_new_daily_image_relative_rootview);
        iv_cover = $(R.id.item_act_new_daily_image_iv_cover);
        relative_checkbox = $(R.id.item_act_new_daily_image_relative_checkbox);
        iv_delete = $(R.id.item_act_new_daily_image_iv_delete);
        //----
        image_height_root = (ScreenUtils.getScreenWidth((Activity) mContext) - ScreenUtils.dip2px(mContext, 30)) / activity.getNumColomn();
        image_height = (ScreenUtils.getScreenWidth((Activity) mContext) - ScreenUtils.dip2px(mContext, 30)) / activity.getNumColomn()-ScreenUtils.dip2px(mContext, 6);
        relative_root.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, image_height_root));
        iv_cover.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, image_height));
    }

    @Override
    public void setData(final String image) {
        imageList = ((RecyclerArrayAdapter<String>) getOwnerAdapter()).getAllData();
        //----最后一张图片功能为添加图片，不能删除
        if (getDataPosition() == imageList.size() - 1) {
            relative_rootview.setBackgroundResource(0);
            relative_rootview.setPadding(0,0,0,0);
            relative_checkbox.setVisibility(View.GONE);
            Glide.with(mContext).load(Integer.parseInt(image))
                    .asBitmap().diskCacheStrategy(DiskCacheStrategy.NONE).centerCrop().into(iv_cover);

        } else {
            relative_rootview.setPadding(ScreenUtils.dip2px(mContext,1),ScreenUtils.dip2px(mContext,1),ScreenUtils.dip2px(mContext,1),ScreenUtils.dip2px(mContext,1));
            relative_rootview.setBackgroundResource(R.drawable.shape_act_new_daily_edit);
            //是否显示删除按钮
            if (activity.isDelete()) {
                relative_checkbox.setVisibility(View.VISIBLE);
                relative_checkbox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        activity.onItemDeleteClick(getDataPosition());
                    }
                });
                iv_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        activity.onItemDeleteClick(getDataPosition());
                    }
                });
            } else {
                relative_checkbox.setVisibility(View.GONE);
            }
            Glide.with(mContext).load(image)
                    .asBitmap().diskCacheStrategy(DiskCacheStrategy.NONE).centerCrop().into(iv_cover);
        }
    }

}
